package org.iteam.core;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.iteam.core.model.base.BaseParameter;
import org.iteam.core.service.base.BaseParameterService;

/**
 * 
 * @Title: Constant.java
 * @Description:常量枚举类
 * @author arts
 * @version V1.0
 */
public final class Constant {
	public static final Integer PAGE_SIZE = 10;
	// 默认密码
	public static final String DEFAULT_PWD = "123456";
	public static DataBaseType dataBaseType = null;
	private static Map<String, String> PARAMS = new HashMap<String, String>();
	static {
		if (dataBaseType == null)
			dataBaseType = DataBaseType.ORACLE;
		try {
			List<BaseParameter> par = BaseParameterService.getInstance()
					.query();
			for (BaseParameter baseParameter : par) {
				PARAMS.put(baseParameter.getName().trim(), baseParameter
						.getValue().trim());
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}

	public static String getParam(String name) {
		return PARAMS.get(name);
	}

	/**
	 * 当前用户激活的角色
	 */
	public static final String SESSION_CURRENT_ROLE_CODE = "SESSION_CURRENT_ROLE_CODE";
	public static final String SESSION_CURRENT_USER = "SESSION_CURRENT_USER";
	
	public enum DataBaseType {
		MYSQL("MYSQL", "mysql"), ORACLE("ORACLE", "oracle");
		public String ecode;
		public String ename;

		private DataBaseType(String code, String vname) {
			this.ecode = code;
			this.ename = vname;
		}

		public String getEcode() {
			return ecode;
		}

		public void setEcode(String ecode) {
			this.ecode = ecode;
		}

		public String getEname() {
			return ename;
		}

		public void setEname(String ename) {
			this.ename = ename;
		}

		public static DataBaseType fromCode(String code) {
			try {
				return DataBaseType.valueOf(code);
			} catch (Exception e) {
				return DataBaseType.MYSQL;
			}
		}
	}

	/**
	 * 性别
	 * 
	 * @author arts
	 * 
	 */
	public enum Sex {
		MALE("MALE", "男"), FEMALE("FEMALE", "女"), UNDEFINED("UNDEFINED", "保密");
		public String ecode;
		public String ename;

		private Sex(String code, String vname) {
			this.ecode = code;
			this.ename = vname;
		}

		public String getEcode() {
			return ecode;
		}

		public void setEcode(String ecode) {
			this.ecode = ecode;
		}

		public String getEname() {
			return ename;
		}

		public void setEname(String ename) {
			this.ename = ename;
		}

		public static Sex fromCode(String code) {
			try {
				return Sex.valueOf(code);
			} catch (Exception e) {
				return Sex.UNDEFINED;
			}
		}
	}

	public enum IsPreset {
		YES("YES", "是"), NO("NO", "否");
		public String ecode;
		public String ename;

		private IsPreset(String code, String vname) {
			this.ecode = code;
			this.ename = vname;
		}

		public String getEcode() {
			return ecode;
		}

		public void setEcode(String ecode) {
			this.ecode = ecode;
		}

		public String getEname() {
			return ename;
		}

		public void setEname(String ename) {
			this.ename = ename;
		}

		public static IsPreset fromCode(String code) {
			try {
				return IsPreset.valueOf(code);
			} catch (Exception e) {
				return IsPreset.NO;
			}
		}
	}

	public static class Permission {
		public enum Mode {
			PROHIBIT("PROHIBIT", "禁止"), PERMIT("PERMIT", "允许");
			public String ecode;
			public String ename;

			private Mode(String code, String vname) {
				this.ecode = code;
				this.ename = vname;
			}

			public String getEcode() {
				return ecode;
			}

			public void setEcode(String ecode) {
				this.ecode = ecode;
			}

			public String getEname() {
				return ename;
			}

			public void setEname(String ename) {
				this.ename = ename;
			}

			public static Mode fromCode(String code) {
				try {
					return Mode.valueOf(code);
				} catch (Exception e) {
					return Mode.PERMIT;
				}
			}
		}
	}

	public static class Function {
		public enum Mode {
			AUTH("AUTH", "验证权限"), LOGIN("LOGIN", "只验证登录"), NOAUTH("NOAUTH",
					"不验证权限");
			public String ecode;
			public String ename;

			private Mode(String code, String vname) {
				this.ecode = code;
				this.ename = vname;
			}

			public String getEcode() {
				return ecode;
			}

			public void setEcode(String ecode) {
				this.ecode = ecode;
			}

			public String getEname() {
				return ename;
			}

			public void setEname(String ename) {
				this.ename = ename;
			}

			public static Mode fromCode(String code) {
				try {
					return Mode.valueOf(code);
				} catch (Exception e) {
					return Mode.NOAUTH;
				}
			}
		}

		public enum Type {
			MENU("MENU", "菜单"), MODULE("MODULE", "模块"), FUNCTION("FUNCTION",
					"功能");
			public String ecode;
			public String ename;

			private Type(String code, String vname) {
				this.ecode = code;
				this.ename = vname;
			}

			public String getEcode() {
				return ecode;
			}

			public void setEcode(String ecode) {
				this.ecode = ecode;
			}

			public String getEname() {
				return ename;
			}

			public void setEname(String ename) {
				this.ename = ename;
			}

			public static Type fromCode(String code) {
				try {
					return Type.valueOf(code);
				} catch (Exception e) {
					return Type.FUNCTION;
				}
			}
		}
	}

	/**
	 * 状态
	 * 
	 * @author arts
	 * 
	 */
	public enum Status {
		ENABLE("ENABLE", "启用"), DISABLE("DISABLE", "禁用"), DELETE("DELETE", "删除"), DESTROY(
				"DESTROY", "销毁"), UNDEFINED("UNDEFINED", "未知");
		public String ecode;
		public String ename;

		private Status(String code, String vname) {
			this.ecode = code;
			this.ename = vname;
		}

		public String getEcode() {
			return ecode;
		}

		public void setEcode(String ecode) {
			this.ecode = ecode;
		}

		public String getEname() {
			return ename;
		}

		public void setEname(String ename) {
			this.ename = ename;
		}

		public static Status fromCode(String code) {
			try {
				return Status.valueOf(code);
			} catch (Exception e) {
				return Status.UNDEFINED;
			}
		}
	}

}
